const cloud = require('wx-server-sdk')

cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
})


const db = cloud.database()
const _ = db.command

// 云函数入口函数 更新用户信息
exports.main = async (event, context) => {
    const requestParam = event.data
    let nickName = requestParam.nickName
    let get_integral = requestParam.getIntegral
    let use_integral = requestParam.useIntegral
    let clock_days = requestParam.clockDays
    if (use_integral == 0) {
        return await db.collection('bb_users').where({nick_name: nickName}).update({
            data: {
                has_integral: _.inc(get_integral),
                total_integral: _.inc(get_integral),
                clock_days: _.inc(clock_days),
                used_integral: _.inc(use_integral)
            }
        })
    } else {
        return await db.collection('bb_users').where({nick_name: nickName}).update({
            data: {
                has_integral: _.inc(-use_integral),
                total_integral: _.inc(get_integral),
                clock_days: _.inc(clock_days),
                used_integral: _.inc(use_integral)
            }
        })
    }
}